/*
 * ExplicaInitView.java
 *
 * $Id: ExplicaInitView.java,v 1.2 2009-03-02 15:31:23 mario Exp $
 */
package org.ceteca.explica.client.gui;

import java.io.InputStream;
import java.net.URL;

import org.ceteca.explica.client.ClientContext;
import org.ceteca.explica.core.InterfaceExplica;
import org.ceteca.javaglade.JavaGlade;
import org.ceteca.javaglade.JavaGladeException;
import org.ceteca.javaglade.handlers.GenericHandler;
import org.ceteca.javaglade.toolkit.swt.SwtToolkit;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

/**
 * Helper class that implements the Explica initializing GUI. 
 * It completes the glade file specification to add extra features.
 * It also implements the event handlers for the Explica initialing screens 
 * and dialogs.
 * <br/>
 * Responsibilities:
 * <ul>
 * 	<li>Definition and inicialization of the Explica init GUI. </li>
 *  <li>Event management for the Explica init windows.</li>
 * </ul>
 * <br/>
 * @author Mario García García <mario@imagos.es>
 * Copyright (c) 2008 Fundación Centro Tecnolóxico da Carne
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * <br/>
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * <br/>
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
public class ExplicaInitView extends GenericHandler {
	/** Unique instance of the ExplicaInitView class. Singleton pattern */
	private static ExplicaInitView instance;

	/** Returns the unique instance of the ExplicaInitView class.
	 * Implements the singleton dessign pattern.
	 * @return ExplicaInitView, unique instance of ExplicaInitView.
	 */
	public static final ExplicaInitView getInstance() {
		if (instance == null)
			instance = new ExplicaInitView();
		return instance;
	}
	
	/**
	 * Private default constructor por ExplicaInitView objects.
	 */
	private ExplicaInitView() {
		ClientContext.getInstance().getLogger().
			info("Building the initializing view of the application...");
		
		// Init the logging system for the JavaGlade module
		ClientContext.getInstance().getLogger().
			debug("Initializing the logging system of the JavaGlade. Configuration file found at: " + 
					this.getClass().getResource("/org/ceteca/javaglade/log4j.xml").getPath());
		JavaGlade.initLogger(null, this.getClass().getResourceAsStream("/org/ceteca/javaglade/log4j.xml"));
	}

	/**
	 * Initializes the Explica Init Viewer engine and builds
	 * the initializing window of the application. Searches automatically for
	 * the glade interface file with the application GUI configuration.
	 */
	public void buildInitView() {
		URL url = this.getClass().getResource("explicainit.glade");
		if (url == null) {
			ClientContext.getInstance().getLogger().
				error("INTERNAL ERROR! NO INTERFACE CONFIGURATION FILE CAN BE FOUND!");
			System.exit(-1);
		}
		
		ClientContext.getInstance().getLogger().
			info(String.format(InterfaceExplica.EXPLICA_LOG_GLADEPATH, url.getPath()));
		
		this.buildInitView(this.getClass().getResourceAsStream("explicainit.glade"), "resources/img");
	}
	
	/**
	 * Initializes the Explica Init Viewer engine and builds
	 * the initializing window of the application.
	 * @param gladeStream InputStream, stream for reading the
	 * Explica init glade file that defines the GUI.
	 * @param resourceDir String, path to the local resources
	 * directory.
	 */
	public void buildInitView(InputStream gladeStream, String resourceDir) {
		// Build the main window of the TraceabilityTreeViewer component
		JavaGlade jGlade = null;
		try {
			ClientContext.getInstance().getLogger().
				info(String.format(InterfaceExplica.EXPLICA_LOG_RESOURCESPATH, resourceDir));
			
			ExplicaGUIBuilder.getInstance().buildGUI("EXPLICA_INIT", gladeStream);
//			ExplicaGUIBuilder.getInstance().printWidgets("EXPLICA_INIT");
		}
		catch (JavaGladeException jEx) {
			ClientContext.getInstance().getLogger().
				error("Error constructing the JavaGlade from the glade init screen file!", jEx);
		}
		catch (Exception ex) {
			ClientContext.getInstance().getLogger().
				error("Error constructing the JavaGlade from the glade init screen file!", ex);
		}
		
		// Set the maximum size of the progress bar
		ProgressBar progressBar = (ProgressBar)ExplicaGUIBuilder.getInstance().getWidget(InterfaceExplica.EXPLICA_INIT_PROGRESSBAR_NAME);
		progressBar.setMaximum(100);
		progressBar.setMinimum(0);
		
		Shell initWindow = ExplicaGUIBuilder.getInstance().getWindow(InterfaceExplica.EXPLICA_INIT_WINDOW_NAME).getShell();
		initWindow.setSize(400, 300);
		initWindow.setMinimumSize(400, 300);
		
		Monitor primary = SwtToolkit.getDisplay().getPrimaryMonitor ();
		Rectangle bounds = primary.getBounds ();
		Rectangle rect = initWindow.getBounds ();
		System.out.println("MONITOR BOUNDS: " + bounds.width + ", " + bounds.height + " px");
		System.out.println("WINDOW BOUNDS: " + rect.width + ", " + rect.height + " px");
		
		int x = bounds.x + (bounds.width - rect.width) / 2;
		int y = bounds.y + (bounds.height - rect.height) / 2;
		System.out.println("WINDOW LOCATION: " + x + ", " + y + " px");
		initWindow.setLocation (x, y);
	}
	
	/**
	 * Returns tag Id assigned to CVS source file.
	 */
	public static String getRevision() {
		return "$Id: ExplicaInitView.java,v 1.2 2009-03-02 15:31:23 mario Exp $";
	}
}